package com.movie.dao;

import com.movie.model.Movie;
import com.movie.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class MovieDao {
    // 获取所有电影
    public List<Movie> getAllMovies() {
        List<Movie> movies = new ArrayList<>();
        String sql = "SELECT * FROM movie";
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                movies.add(new Movie(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("director"),
                        rs.getString("genre"),
                        rs.getDate("release_date")
                ));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return movies;
    }

    // 添加电影
    public void addMovie(Movie movie) {
        String sql = "INSERT INTO movie (name, director, genre, release_date) VALUES (?, ?, ?, ?)";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, movie.getName());
            stmt.setString(2, movie.getDirector());
            stmt.setString(3, movie.getGenre());
            stmt.setDate(4, movie.getReleaseDate());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据电影ID获取电影
    public Movie getMovieById(int id) {
        Movie movie = null;
        String sql = "SELECT * FROM movie WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    movie = new Movie(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getString("director"),
                            rs.getString("genre"),
                            rs.getDate("release_date")
                    );
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return movie;
    }

    // 更新电影
    public void updateMovie(Movie movie) {
        String sql = "UPDATE movie SET name = ?, director = ?, genre = ?, release_date = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, movie.getName());
            stmt.setString(2, movie.getDirector());
            stmt.setString(3, movie.getGenre());
            stmt.setDate(4, movie.getReleaseDate());
            stmt.setInt(5, movie.getId());
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 删除电影
    public void deleteMovie(int id) {
        String sql = "DELETE FROM movie WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);
            stmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    // 根据查询条件获取电影列表
    public List<Movie> getMoviesByCriteria(String name, String director, String genre) {
        List<Movie> movies = new ArrayList<>();

        // 构建动态查询语句
        StringBuilder sql = new StringBuilder("SELECT * FROM movie WHERE 1=1");

        if (name != null && !name.isEmpty()) {
            sql.append(" AND name LIKE ?");
        }
        if (director != null && !director.isEmpty()) {
            sql.append(" AND director LIKE ?");
        }
        if (genre != null && !genre.isEmpty()) {
            sql.append(" AND genre LIKE ?");
        }

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql.toString())) {

            int index = 1;

            // 设置查询条件的参数
            if (name != null && !name.isEmpty()) {
                stmt.setString(index++, "%" + name + "%");
            }
            if (director != null && !director.isEmpty()) {
                stmt.setString(index++, "%" + director + "%");
            }
            if (genre != null && !genre.isEmpty()) {
                stmt.setString(index++, "%" + genre + "%");
            }

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    movies.add(new Movie(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getString("director"),
                            rs.getString("genre"),
                            rs.getDate("release_date")
                    ));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return movies;
    }

    public List<Movie> getMovies(String name, String director, String genre) {
        List<Movie> movies = new ArrayList<>();
        StringBuilder sql = new StringBuilder("SELECT * FROM movie WHERE 1=1");

        if (name != null && !name.isEmpty()) {
            sql.append(" AND name LIKE ?");
        }
        if (director != null && !director.isEmpty()) {
            sql.append(" AND director LIKE ?");
        }
        if (genre != null && !genre.isEmpty()) {
            sql.append(" AND genre LIKE ?");
        }

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql.toString())) {

            int index = 1;
            if (name != null && !name.isEmpty()) {
                stmt.setString(index++, "%" + name + "%");
            }
            if (director != null && !director.isEmpty()) {
                stmt.setString(index++, "%" + director + "%");
            }
            if (genre != null && !genre.isEmpty()) {
                stmt.setString(index++, "%" + genre + "%");
            }

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    movies.add(new Movie(
                            rs.getInt("id"),
                            rs.getString("name"),
                            rs.getString("director"),
                            rs.getString("genre"),
                            rs.getDate("release_date")
                    ));
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return movies;
    }

}
